const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function isArray(o) {
  return Object.prototype.toString.call(o) == '[object Array]';
}

function http(url, method, data, collBack) {
  wx.request({
    url: url,
    method: method,
    data: data,
    header: {
      "Content-Type": "application/json"
    },
    success: (res) => {
      console.log(res);
      if (res.data.status == '1') {
        if (collBack) {
          collBack(res.data);
        }
      } else {
        wx.showToast({
          icon: 'none',
          title: '服务端调用异常[' + res.data.msg + ']',
        })
      }
    },
    fail: (error) => {
      console.log(error);
      wx.showToast({
        icon: 'none',
        title: '抱歉,出现一个错误'
      })
    }
  })
}

function httpform(url, method, data, collBack) {
  wx.request({
    url: url,
    method: method,
    data: data,
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success: (res) => {
      if (res.data.status == '1') {
        collBack(res.data);
      } else {
        wx.showToast({
          title: '调用接口异常' + res.data.msg,
        })
      }
    },
    fail: (error) => {
      wx.showToast({
        title: '调用接口失败' + error,
      })
      console.log(error);
    }
  })
}

module.exports = {
  formatTime: formatTime,
  isArray: isArray,
  http: http,
  httpform: httpform
}